#include "db_grpc_client.h"

int dbClient::userSearchByID(const int& id) {
	dbserver::SearchByIDRequest request;
	request.set_id(id);
	dbserver::SearchByIDResponse response;
	ClientContext context;

	Status status = stub_->UserSearchByID(&context, request, &response);

	if (status.ok()) {
		return response.result();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}
}

std::string dbClient::userGetNameByID(const int& id)
{
	dbserver::GetByIDRequest request;
	request.set_id(id);
	dbserver::GetStringResponse response;
	ClientContext context;

	Status status = stub_->UserGetNameByID(&context, request, &response);

	if (status.ok()) {
		return response.result();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return "none";
	}
}
std::string dbClient::userGetPasswordByID(const int& id)
{
	dbserver::GetByIDRequest request;
	request.set_id(id);
	dbserver::GetStringResponse response;
	ClientContext context;

	Status status = stub_->UserGetPasswordByID(&context, request, &response);

	if (status.ok()) {
		return response.result();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return "none";
	}

}
int dbClient::userUpdateNameByID(const int& id, const std::string& name)
{
	dbserver::UpdateRequest request;
	request.set_id(id);
	request.set_data(name);
	dbserver::UpdateResponse response;
	ClientContext context;

	Status status = stub_->UserUpdateNameByID(&context, request, &response);

	if (status.ok()) {
		return response.success();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}

}
int dbClient::userUpdatePasswordByID(const int& id, const std::string& password)
{
	dbserver::UpdateRequest request;
	request.set_id(id);
	request.set_data(password);
	dbserver::UpdateResponse response;
	ClientContext context;

	Status status = stub_->UserUpdatePasswordByID(&context, request, &response);

	if (status.ok()) {
		return response.success();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}

}
int dbClient::userDeleteByID(const int& id)
{
	dbserver::DeleteByIDRequest request;
	request.set_id(id);
	dbserver::DeleteResponse response;
	ClientContext context;

	Status status = stub_->UserDeleteByID(&context, request, &response);

	if (status.ok()) {
		return response.success();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}

}
int dbClient::userInsert(const int& id, const std::string& name, const std::string& password)
{
	dbserver::InsertRequest request;
	request.set_id(id);
	request.set_name(name);
	request.set_password(password);
	dbserver::InsertResponse response;
	ClientContext context;

	Status status = stub_->UserInsert(&context, request, &response);

	if (status.ok()) {
		return response.success();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}

}

int dbClient::user_infoSearchByID(const int& id)
{
	dbserver::SearchByIDRequest request;
	request.set_id(id);
	dbserver::SearchByIDResponse response;
	ClientContext context;

	Status status = stub_->UserInfoSearchByID(&context, request, &response);

	if (status.ok()) {
		return response.result();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}
}
int dbClient::user_infoInsert(const user_info& uf)
{
	dbserver::UserInfoInsertRequest request;
	request.set_id(uf.id);
	request.set_age(uf.age);
	request.set_email(uf.email);
	request.set_phone(uf.phone);
	request.set_sex(uf.sex);
	request.set_gtime(uf.gtime);
	request.set_info(uf.info);
	dbserver::InsertResponse response;
	ClientContext context;

	Status status = stub_->UserInfoInsert(&context, request, &response);

	if (status.ok()) {
		return response.success();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}
}
int dbClient::user_infoDeleteByID(const int& id)
{
	dbserver::DeleteByIDRequest request;
	request.set_id(id);
	dbserver::DeleteResponse response;
	ClientContext context;

	Status status = stub_->UserInfoDeleteByID(&context, request, &response);

	if (status.ok()) {
		return response.success();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}
}
int dbClient::user_infoUpdateByID(const user_info& uf)
{
	dbserver::UserInfoInsertRequest request;
	request.set_id(uf.id);
	request.set_age(uf.age);
	request.set_email(uf.email);
	request.set_phone(uf.phone);
	request.set_sex(uf.sex);
	request.set_gtime(uf.gtime);
	request.set_info(uf.info);
	dbserver::UpdateResponse response;
	ClientContext context;

	Status status = stub_->UserInfoUpdateByID(&context, request, &response);

	if (status.ok()) {
		return response.success();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}
}
dbClient::user_info dbClient::user_infoGetInfoByID(const int& id)
{
	dbserver::GetByIDRequest request;
	request.set_id(id);
	dbserver::UserInfoResponse response;
	ClientContext context;

	Status status = stub_->UserInfoGetInfoByID(&context, request, &response);
	user_info uf;
	uf.id = response.id();
	uf.age = response.age();
	uf.email = response.email();
	uf.phone = response.phone();
	uf.sex = response.sex();
	uf.info = response.info();
	uf.gtime = response.gtime();

	if (status.ok()) {
		return uf;
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return user_info();
	}
}



int dbClient::server_logSearchByID(const int& id)
{
	dbserver::SearchByIDRequest request;
	request.set_id(id);
	dbserver::SearchByIDResponse response;
	ClientContext context;

	Status status = stub_->ServerLogSearchByID(&context, request, &response);

	if (status.ok()) {
		return response.result();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}
}
int dbClient::server_logInsert(const server_log& sl)
{
	dbserver::ServerLogInsertRequest request;
	request.set_id(sl.id);
	request.set_level(sl.level);
	request.set_data(sl.data);
	request.set_time(sl.time);
	dbserver::InsertResponse response;
	ClientContext context;

	Status status = stub_->ServerLogInsert(&context, request, &response);

	if (status.ok()) {
		return response.success();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}
}
int dbClient::server_logDeleteByID(const int& id)
{
	dbserver::DeleteByIDRequest request;
	request.set_id(id);
	dbserver::DeleteResponse response;
	ClientContext context;

	Status status = stub_->ServerLogDeleteByID(&context, request, &response);

	if (status.ok()) {
		return response.success();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}
}
int dbClient::server_logUpdateByID(const server_log& sl)
{
	dbserver::ServerLogInsertRequest request;
	request.set_id(sl.id);
	request.set_data(sl.data);
	request.set_level(sl.level);
	request.set_time(sl.time);
	dbserver::UpdateResponse response;
	ClientContext context;

	Status status = stub_->ServerLogUpdateByID(&context, request, &response);

	if (status.ok()) {
		return response.success();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}
}
dbClient::server_log dbClient::server_logGetLogByID(const int& id)
{
	dbserver::GetByIDRequest request;
	request.set_id(id);
	dbserver::ServerLogResponse response;
	ClientContext context;

	Status status = stub_->ServerLogGetLogByID(&context, request, &response);

	server_log sl;
	sl.id = response.id();
	sl.time = response.time();
	sl.data = response.data();
	sl.level = response.level();

	if (status.ok()) {
		return sl;
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return server_log();
	}
}




int dbClient::message_logSearchByID(const int& id)
{
	dbserver::SearchByIDRequest request;
	request.set_id(id);
	dbserver::SearchByIDResponse response;
	ClientContext context;

	Status status = stub_->MessageLogSearchByID(&context, request, &response);

	if (status.ok()) {
		return response.result();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}
}
std::string dbClient::message_logGetMessageByUserID(const int& id)
{
	dbserver::GetByIDRequest request;
	request.set_id(id);
	dbserver::GetStringResponse response;
	ClientContext context;

	Status status = stub_->MessageLogGetMessageByUserID(&context, request, &response);

	if (status.ok()) {
		return response.result();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return "";
	}
}
int dbClient::message_logInsert(const message_log& ml)
{
	dbserver::MessageLogInsertRequest request;
	request.set_id(ml.id);
	request.set_receive_id(ml.receive_id);
	request.set_send_id(ml.send_id);
	request.set_status(ml.status);
	request.set_data(ml.data);
	request.set_time(ml.time);
	dbserver::InsertResponse response;
	ClientContext context;

	Status status = stub_->MessageLogInsert(&context, request, &response);

	if (status.ok()) {
		return response.success();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}
}

int dbClient::friend_tableSearchByID(const int& id)
{
	dbserver::SearchByIDRequest request;
	request.set_id(id);
	dbserver::SearchByIDResponse response;
	ClientContext context;

	Status status = stub_->FriendTableSearchByID(&context, request, &response);

	if (status.ok()) {
		return response.result();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}
}
std::string dbClient::friend_tableGetFriendsByUserID(const int& id)
{
	dbserver::GetByIDRequest request;
	request.set_id(id);
	dbserver::GetStringResponse response;
	ClientContext context;

	Status status = stub_->FriendTableGetFriendsByUserID(&context, request, &response);

	if (status.ok()) {
		return response.result();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return "";
	}
}
int dbClient::friend_tableIsFriend(const int& ida, const int& idb)
{
	dbserver::IsFriendRequest request;
	request.set_ida(ida);
	request.set_idb(idb);
	dbserver::IsFriendResponse response;
	ClientContext context;

	Status status = stub_->FriendTableIsFriend(&context, request, &response);

	if (status.ok()) {
		return response.is_friend();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}
}
int dbClient::friend_tableDeleteByUserID(const int& ida, const int& idb)
{
	dbserver::DeleteByUserIDRequest request;
	request.set_ida(ida);
	request.set_idb(idb);
	dbserver::DeleteResponse response;
	ClientContext context;

	Status status = stub_->FriendTableDeleteByUserID(&context, request, &response);

	if (status.ok()) {
		return response.success();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}
}
int dbClient::friend_tableInsert(const friend_table& ft)
{
	dbserver::FriendTableInsertRequest request;
	request.set_friend_id(ft.friend_id);
	request.set_user_id_a(ft.user_id_a);
	request.set_user_id_b(ft.user_id_b);
	request.set_friend_time(ft.friend_time);

	dbserver::InsertResponse response;
	ClientContext context;

	Status status = stub_->FriendTableInsert(&context, request, &response);

	if (status.ok()) {
		return response.success();
	}
	else {
		std::cout << "RPC failed: " << status.error_message() << std::endl;
		return -1;
	}
}